package com.likerSai;

import java.util.Stack;

/**
 * 有效的括号
 *
 * 看错了，还以为是有其他字符
 * @author sun.ye
 * @since 2022-03-25 14:04
 */
public class IsValid {

    public static void main (String[] args) {
        boolean result = isValid("]");
        System.out.println(result);
    }



    public static boolean isValid(String s) {
        Stack<Character> characterStack = new Stack<>();
//        '('，')'，'{'，'}'，'['，']'
        Character topC;
        for (Character c : s.toCharArray()) {
            if ("({[".indexOf(c) >= 0)  {
                characterStack.push(c);
            }else if (")]}".indexOf(c) >= 0){

                if (characterStack.isEmpty()){
                    return false;
                }
                topC = characterStack.pop();
                if ('(' == topC && ')' == c){
                    continue;
                }
                if ('{' == topC && '}' == c){
                    continue;
                }
                if ('[' == topC && ']' == c){
                    continue;
                }
                return false;
            }
        }

        return characterStack.isEmpty();

    }

}